package com.loong.musicdemo.ui.video;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.ui.video.activity.MoreMvActivity;
import com.loong.musicdemo.ui.video.activity.SearchMvActivity;
import com.loong.musicdemo.ui.video.activity.WonderfulMvActivity;
import com.loong.musicdemo.adapter.NormalMVAdapter;
import com.loong.musicdemo.adapter.WonderfulMVAdapter;
import com.loong.musicdemo.util.dataDeal.MvDeal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class VideoFragment extends Fragment implements View.OnClickListener {
    private static final int LIMIT = 10;//每次获取数量
    private static int page = 0;//初始页 offset = page * LIMIT
    private Button moreMV, mvClassification;//更多MV与分类MV列表
    private RecyclerView featuredMV, wonderfulMV;//精选MV\精彩MV
    private static WonderfulMVAdapter wonderfulMVAdapter;
    private static NormalMVAdapter normalMVAdapter;
    private LinearLayout searchMv;//搜索框
    @SuppressLint("StaticFieldLeak")
    private static SwipeRefreshLayout refreshMVData;//刷新数据
    @SuppressLint("StaticFieldLeak")
    private static SmartRefreshLayout loadMVData;//加载数据
    private static final List<MV> featuredMVs = new ArrayList<>();
    private static final List<MV> wonderfulMVs = new ArrayList<>();

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_video, container, false);
        initView(root);//初始化视图
        normalMVAdapter = new NormalMVAdapter(featuredMVs);
        featuredMV.setAdapter(normalMVAdapter);
        wonderfulMVAdapter = new WonderfulMVAdapter(wonderfulMVs);
        wonderfulMV.setAdapter(wonderfulMVAdapter);
        initEvent();//初始化事件
        initData();//初始化数据
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Glide.with(this).onLowMemory();
    }

    /**
     * 初始化MV数据
     */
    private void initData() {
        new InitFeaturedMV().execute();
        new InitWonderfulMv().execute();
        refreshMVData.setRefreshing(false);
    }

    /**
     * 用于获取热门推荐MV数据
     */
    static class InitFeaturedMV extends AsyncTask<Void, Void, List<MV>> {

        @Override
        protected List<MV> doInBackground(Void... voids) {
            return MvDeal.getMVByUrl("/personalized/mv");
        }

        @Override
        protected void onPostExecute(List<MV> mvs) {
            featuredMVs.clear();//清空原有数据
            if (mvs != null && mvs.size() != 0) {
                featuredMVs.addAll(mvs);
                normalMVAdapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 初始化获取精彩MV数据
     */
    static class InitWonderfulMv extends AsyncTask<Void, Void, List<MV>> {

        @Override
        protected List<MV> doInBackground(Void... voids) {
            return MvDeal.getMVByOffsetAndArea("全部", page++, LIMIT);
        }

        @Override
        protected void onPostExecute(List<MV> mvs) {
            wonderfulMVs.clear();
            if (mvs != null && mvs.size() != 0) {
                wonderfulMVs.addAll(mvs);
                wonderfulMVAdapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * 加载MV数据
     */
    static class LoadWonderfulMV extends AsyncTask<Void, Void, List<MV>> {

        @Override
        protected List<MV> doInBackground(Void... voids) {
            return MvDeal.getMVByOffsetAndArea("全部", page++, LIMIT);
        }

        @Override
        protected void onPostExecute(List<MV> mvs) {
            if (mvs != null && mvs.size() != 0) {
                wonderfulMVs.addAll(mvs);
                wonderfulMVAdapter.notifyDataSetChanged();
            }
            loadMVData.finishLoadmore();//停止加载显示
        }
    }

    /**
     * 初始化绑定事件
     */
    private void initEvent() {
        moreMV.setOnClickListener(this);
        mvClassification.setOnClickListener(this);
        //搜索框点击处理
        searchMv.setOnClickListener(this);
        //下拉刷新
        refreshMVData.setOnRefreshListener(() -> {
            page = 0;//清空页数
            initData();
        });
        //上啦加载
        loadMVData.setOnLoadmoreListener(refreshLayout -> {
            new LoadWonderfulMV().execute();//添加数据
        });
    }

    /**
     * 初始化视图
     *
     * @param root 父容器
     */
    private void initView(View root) {
        moreMV = root.findViewById(R.id.mv_more);
        mvClassification = root.findViewById(R.id.mv_classification);
        featuredMV = root.findViewById(R.id.featuredMV);
        wonderfulMV = root.findViewById(R.id.wonderfulMV);
        //搜索
        searchMv = root.findViewById(R.id.search);
        //刷新
        refreshMVData = root.findViewById(R.id.refresh_mv);
        //加载
        loadMVData = root.findViewById(R.id.load_mv);
        loadMVData.setEnableRefresh(false);//禁用下拉
        loadMVData.autoLoadmore();//加载更多
    }

    /**
     * 点击事件
     *
     * @param v 点击视图
     */
    @Override
    public void onClick(View v) {
        Intent intent;//跳转目标
        switch (v.getId()) {
            case R.id.mv_more:
                intent = new Intent(getContext(), MoreMvActivity.class);
                if (isAdded()) {
                    startActivity(intent);
                }
                break;
            case R.id.mv_classification:
                intent = new Intent(getContext(), WonderfulMvActivity.class);
                if (isAdded()) {
                    startActivity(intent);
                }
                break;
            case R.id.search:
                intent = new Intent(getContext(), SearchMvActivity.class);
                if (isAdded()) {
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }
}

